print("202509021401")
-- 测试转发服务格式
-- 根据用户提供的地址格式进行测试

local HttpService = game:GetService("HttpService")

-- 安全的文件写入函数
local function safeWriteFile(filename, content)
    local success, err = pcall(function()
        appendfile(filename, content)
    end)
    
    if not success then
        local writeSuccess, writeErr = pcall(function()
            writefile(filename, content)
        end)
        
        if not writeSuccess then
            print("文件写入失败: " .. filename .. " - " .. tostring(writeErr))
        end
    end
end

-- 测试1: 使用用户提供的格式测试基本连接
local function testBasicForward()
    print("=== 测试1: 基本转发连接 ===")
    
    local success, err = pcall(function()
        local url = "https://forward.shopwegame.com/forward?url=https://lokipush.shopwegame.com/loki/api/v1/push&method=POST&data=%7B%22streams%22%3A%5B%7B%22stream%22%3A%7B%22job%22%3A%22my-app%22%7D%2C%22values%22%3A%5B%5B%221756791704000000000%22%2C%22%E4%B8%AD%E6%96%87%E6%B5%8B%E8%AF%95%E4%BF%A1%E6%81%AF1756791704%22%5D%5D%7D%5D%7D"
        
        print("测试URL: " .. url)
        
        local response = game:HttpGet(url, true)
        
        print("响应: " .. response)
        
        -- 记录结果
        safeWriteFile("forward_test_basic.txt", string.format("[%s] 基本转发测试结果: %s\n", os.date("%Y-%m-%d %H:%M:%S"), response))
        
        return response
    end)
    
    if not success then
        print("基本转发测试失败: " .. tostring(err))
        safeWriteFile("forward_test_basic_error.txt", string.format("[%s] 基本转发测试失败: %s\n", os.date("%Y-%m-%d %H:%M:%S"), tostring(err)))
    end
end

-- 测试3: 使用 Loki 标准格式
local function testLokiForward()
    print("=== 测试3: Loki 标准格式测试 ===")
    
    local success, err = pcall(function()
        -- 获取当前时间戳（纳秒级）
        local timestamp = os.time() * 1000000000
        -- 构建 Loki 标准数据
        local lokiData = {
            streams = {
                {
                    stream = {
                        app = "u7playearn",
                        log_type = "test",
                        username = "pssmurf0078",
                        order_id = "2025",
                        order_status = "2"
                    },
                    values = {
                        { tostring(timestamp), "标准格式测试日志消息 - " .. os.date("%Y-%m-%d %H:%M:%S") }
                    }
                }
            }
        }
        
        -- 转换为 JSON 并 URL 编码
        local jsonData = HttpService:JSONEncode(lokiData)
        local encodedData = HttpService:UrlEncode(jsonData)
        
        print("Loki 数据: " .. jsonData)
        print("编码后: " .. encodedData)
        
        -- 构建转发请求参数
        local forwardParams = {
            url = "https://lokipush.shopwegame.com/loki/api/v1/push",
            method = "POST",
            data = encodedData
        }
        
        -- 构建查询字符串
        local queryParts = {}
        for key, value in pairs(forwardParams) do
            table.insert(queryParts, key .. "=" .. tostring(value))
        end
        local queryString = table.concat(queryParts, "&")
        
        -- 构建完整 URL
        local url = "https://forward.shopwegame.com/forward?" .. queryString
        
        print("Loki 转发URL: " .. url)
        
        -- 发送请求
        local response = game:HttpGet(url, true)
        
        print("响应: " .. response)
        
        -- 记录结果
        safeWriteFile("forward_test_loki.txt", string.format("[%s] Loki 转发测试结果: %s\n", os.date("%Y-%m-%d %H:%M:%S"), response))
        
        return response
    end)
    
    if not success then
        print("Loki 转发测试失败: " .. tostring(err))
        safeWriteFile("forward_test_loki_error.txt", string.format("[%s] Loki 转发测试失败: %s\n", os.date("%Y-%m-%d %H:%M:%S"), tostring(err)))
    end
end

-- 主测试函数
local function runAllTests()
    print("开始测试转发服务格式...")
    
    testLokiForward()
    print("\n")
    
    print("所有测试完成！")
end

-- 运行测试
runAllTests()
